package com.wzx.data.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wzx.basics.baseVo.PageVo;
import com.wzx.basics.baseVo.Result;
import com.wzx.basics.utils.PageInfo;
import com.wzx.basics.utils.PageUtil;
import com.wzx.basics.utils.ResultUtil;
import com.wzx.data.entity.Course;
import com.wzx.data.service.ICourseService;
import com.wzx.data.utils.NullUtils;
import com.wzx.data.entity.Collection;
import com.wzx.data.service.ICollectionService;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 收藏 前端控制器
 * @author wzx
 * @since 2023/03/20
 */
@Slf4j
@RestController
@Api(tags = "收藏管理接口")
@RequestMapping("/wzx/collection")
@Transactional
public class CollectionController {

    @Autowired
    private ICollectionService iCollectionService;

    @Autowired
    private ICourseService iCourseService;

    @RequestMapping(value = "/check", method = RequestMethod.GET)
    @ApiOperation(value = "查询是否已经收藏")
    public Result check(Collection collection ){
        LambdaQueryWrapper<Collection> lambda = new LambdaQueryWrapper<Collection>();
        lambda.eq(Collection::getCourseId,collection.getCourseId())
                .eq(Collection::getUserId,collection.getUserId());
        Collection one = iCollectionService.getOne(lambda);
        if(one != null){
            return ResultUtil.data(true);
        }
        return ResultUtil.data(false);
    }

    @RequestMapping(value = "/getOne", method = RequestMethod.GET)
    @ApiOperation(value = "查询单条收藏")
    public Result<Collection> get(@RequestParam String id){
        return new ResultUtil<Collection>().setData(iCollectionService.getById(id));
    }

    @RequestMapping(value = "/count", method = RequestMethod.GET)
    @ApiOperation(value = "查询全部收藏个数")
    public Result<Long> getCount(){
        return new ResultUtil<Long>().setData(iCollectionService.count());
    }

    @RequestMapping(value = "/getAll", method = RequestMethod.GET)
    @ApiOperation(value = "查询全部收藏")
    public Result<List<Collection>> getAll(){
        return new ResultUtil<List<Collection>>().setData(iCollectionService.list());
    }

    @RequestMapping(value = "/getByPage", method = RequestMethod.GET)
    @ApiOperation(value = "查询收藏")
    public Result getByPage(@ModelAttribute Collection collection ,@ModelAttribute PageVo pageVo){
        PageInfo<Course> pageInfo = iCollectionService.getListData(collection,pageVo);
        return new ResultUtil().setData(pageInfo);
    }

    @RequestMapping(value = "/insert", method = RequestMethod.POST)
    @ApiOperation(value = "新增收藏")
    public Result<Collection> insert(Collection collection){
        iCollectionService.saveOrUpdate(collection);
        return new ResultUtil<Collection>().setData(collection);
    }

    @RequestMapping(value = "/delByIds", method = RequestMethod.POST)
    @ApiOperation(value = "删除收藏")
    public Result<Object> delByIds(@RequestParam String id){
        iCollectionService.removeById(id);
        return ResultUtil.success();
    }
    /*
    collect(id) {
      this.$confirm('是否确定收藏课程?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(
          () => {
            this.$axios.post("/collect/course",{
              courseId: id,
              userId: 1,
            }).then(res => {
              if (res && res.status === 200) {
                this.isCollect = true;
                this.$message({
                  type: 'success',
                  message: '已收藏成功'
                })
              }
            })
          }
      )
    },
    */
}
